package icu.zhhll.utils;

import icu.zhhll.util.io.StreamUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author zh
 * @date 2023/8/14 17:15
 */
public class HttpHelper {
    /**
     * 获取请求体
     *
     * @param request
     * @param encoding
     * @return
     * @throws IOException
     */
    public static String getBodyString(HttpServletRequest request, String encoding) throws IOException {
        InputStream inputStream = request.getInputStream();
        String strData = StreamUtils.readStream(inputStream, encoding);
        if (inputStream != null) {
            inputStream.close();
        }
        return strData;
    }

    /**
     * 获取请求体
     *
     * @param request
     * @return
     * @throws IOException
     */
    public static byte[] getBody(HttpServletRequest request) throws IOException {
        InputStream inputStream = request.getInputStream();
        byte[] data = StreamUtils.readStreamToBytes(inputStream);
        if (inputStream != null) {
            inputStream.close();
        }
        return data;
    }
}
